import urllib.request
import re
import xlwt
from requests import RequestException


def get_info():

    url_list = []
    for i in range(10):
        url = 'https://wuhan.anjuke.com/sale/p{}/#filtersort'.format(i)
        headers = ("User-Agent",
                   "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
                   "(KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 "
                   "QQBrowser/10.3.2854.400")
        opener = urllib.request.build_opener()
        opener.addheaders = [headers]
        try:
            urllib.request.install_opener(opener)
            html = urllib.request.urlopen(url).read().decode("utf-8")

        except RequestException:
            return None
        pattern = re.compile('<a data-from.*?href="(.*?)".*?</a>.*?<span>(.*?)</span>.*?'
                             + '<span>(.*?)</span>.*?<span>(.*?)</span>.*?<span>(.*?)</span>.*?</span>'
                             + '.*?</span>.*?price-det.*?>(\d+)</strong>', re.S)
        page_list = re.findall(pattern, html)
        url_list.append(page_list)
    return url_list


def excel_write(items):
    newTable = '安居客二手房.xls'
    wb = xlwt.Workbook(encoding='utf-8')
    ws = wb.add_sheet('test1')
    headData = ['二手房网址', '房间构造', '面积', '楼层', '建造时间', '价格/万']
    for colnum in range(0, 6):
        ws.write(0, colnum, headData[colnum], xlwt.easyxf('font:bold on'))
    wb.save(newTable)
    print('创建成功')

    index = 1
    for item in items:
        for j in range(0, len(item)):
            for i in range(0, 6):
                print(item[j][i])
                ws.write(index, i, item[j][i])
            index += 1
        wb.save(newTable)


if __name__ == '__main__':
    lists = get_info()
excel_write(lists)
